<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/9/5
 * Time: 11:05
 */

/**
 * Created by wanglu on 2017/9/5.
 */
class Project_version_service extends MY_Service
{


    public function __construct()
    {
        parent::__construct();
        $this->load->model('project_model');
        $this->load->model('lang_model');
        $this->load->model('key_model');
        $this->load->model('project_user_model');
        $this->load->model('lang_user_model');
        $this->load->model('project_content_temp_model');
        $this->load->model('project_lang_version_model');
        $this->load->model('project_version_model');
        $this->load->model('project_content_history_model');
    }

    public function get_project_verson_by_id($id) {
         return $this->project_version_model->getRecord(array('id'=> $id));
    }


    public function get_lang_history_content($pid, $lang_id, $lang_version ) {
        return $this->project_content_history_model->getRecord(array('pid'=> $pid, 'lang_id'=> $lang_id, 'lang_version'=> $lang_version));
    }

    public function submit($opuser, $pid, $lang_ids = array(), $remark, $user_project_relation) {

        // 翻译员  需要权限校验

        if ($user_project_relation == AppConstants::PROJECT_ROLE_INTERPRETER) {
            foreach ($lang_ids as $lang_id) {
                $user_lang = $this->lang_user_model->getRecord(array('uid'=> $opuser['id'], 'pid'=> $pid, 'lang_id'=> $lang_id));
                $lang = $this->lang_model->getRecord(array('id'=> $lang_id));
                if ($user_lang['permission'] != AppConstants::LANG_LANG_PERMISSION_READ_WRITE) {
                    throw  new AppException(ErrorCode::FAIL, $lang['name']." has no  permission !");
                }
            }
        }


        $last_project_version = $this->project_version_model->get_max_version($pid);

        if (empty($last_project_version)) {
            $last_project_version =  1;
        } else {
            $last_project_version = $last_project_version + 1;
        }

        $submit_langs = array();
        /**
         * 语言版本升级+1
         */
        foreach ($lang_ids as $lang_id) {
            $lang = $this->lang_model->getRecord(array('id'=> $lang_id));
            $submit_langs[] = $lang;
            $last_lang_version = $this->project_lang_version_model->get_max_version($pid, $lang_id);
            if (empty($last_lang_version)) {
                $last_lang_version = 1;
            } else {
                $last_lang_version = $last_lang_version + 1;
            }

            $lang_version_data = array(
                'uid' => $opuser['id'],
                'lang_id' => $lang_id,
                'version' => $last_lang_version,
                'pid' => $pid,
                'createtime' => time(),
                'shorthand' => $lang['shorthand'],
                'remark' => $remark,
                'username'=> $opuser['username']
            );


            //存入版本记录
            $lang_temp_content = $this->get_lang_temp_content($pid, $lang_id);

            $content_history_data = array(
                'pid'=> $pid,
                'lang_id'=> $lang_id,
                'content'=> json_encode($lang_temp_content),
                'lang_version'=> $last_lang_version,
                'project_version'=> $last_project_version,
                'createtime'=> time(),
                'shortlang'=> $lang['shorthand'],
                'uid'=> $opuser['id']
            );

            $this->project_content_history_model->createRecord($content_history_data);
            //插入语言版本信息
            $this->project_lang_version_model->createRecord($lang_version_data);
        }


        $lang_versions = $this->get_project_lang_versions($pid);




        $pkeys = $this->key_model->get_pkeys($pid);

        $project_data = array(
            'uid'=> $opuser['id'],
            'pid'=> $pid,
            'version'=> $last_project_version,
            'lang_version_info' => json_encode($lang_versions),
            'remark'=> $remark,
            'createtime'=> time(),
            'pkey_info'=> json_encode($pkeys),
            'username'=> $opuser['username'],
            'submit_lang' => json_encode($submit_langs)
        );

        //存入项目版本信息
       return  $this->project_version_model->createRecord($project_data);
    }

    /**
     * 获取语言版本信息
     * @param $pid
     * @return array
     */
    public function get_project_lang_versions($pid) {
        $langs = $this->lang_model->getRecords(array('pid'=> $pid));
        $lang_versions = array();
        foreach ($langs as $lang) {
            $lang_v = $this->project_lang_version_model->get_max_version($pid, $lang['id']);
            if (empty($lang_v)) {
                $lang_v = 0;
            }
            $lang_versions[]=  array(
                'id'=> $lang['id'],
                'shorthand' => $lang['shorthand'],
                'version'=> $lang_v,
                'name'=> $lang['name'],
                'remark'=> $lang['remark']
            );
        }
        return $lang_versions;
    }
    /**
     * 获取当前语言的编辑值
     * @param $pid
     * @param $lang_id
     * @return array
     */
    public function  get_lang_temp_content($pid, $lang_id) {
        $temp_contents =  $this->project_content_temp_model->get_temp_contents($pid, $lang_id);

        $new_temp_contents = array();
        foreach ($temp_contents as $tc) {
            $new_temp_contents[$tc['pkey']] = $tc;
        }
        
        $pkeys = $this->key_model->get_pkeys($pid);
        /**
         * "pkey":"INTRO_START","display_order":"1","limit_length":"-1","remark":"","content":"\u5f00\u59cb"
         */
        $merge_temp_contetns = array();
        foreach ($pkeys as $pkey) {
            //空的赋值
            if (empty($new_temp_contents[$pkey['pkey']])) {
                $merge_temp_contetns[$pkey['pkey']] = array(
                    'pkey'=> $pkey['pkey'],
                    'content'=> '',
                    'display_order'=> $pkey['display_order'],
                    'limit_length'=> $pkey['limit_length'],
                    'remark' => $pkey['remark']
                );
            } else {
                $merge_temp_contetns[$pkey['pkey']] = $new_temp_contents[$pkey['pkey']];
            }
        }
        return $merge_temp_contetns;
    }


    public function get_project_version($start, $length, $pid, $order_by_createtime)
    {

        $this->db->where('pid', $pid);
        $orderby = array("createtime" => $order_by_createtime);
        return $this->project_version_model->getPageData($start, $length, $orderby);
    }
}
